package ui;
import system.GameLogic;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

// 前端界面类，负责界面展示和用户交互
public class LifeSimulatorGUI extends JFrame {
    private GameLogic gameLogic;
    private JLabel ageLabel;
    private JLabel healthLabel;
    private JLabel wealthLabel;
    private JLabel happinessLabel;
    private JLabel marriageLabel;
    private JLabel childrenLabel;
    private JLabel employmentLabel;
    private JLabel nameLabel;
    private JLabel spouseLabel;
    private JTextArea eventTextArea;

    public LifeSimulatorGUI() {
        this.gameLogic = new GameLogic();
        setTitle("人生模拟器");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 信息面板
        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new GridLayout(9, 1));
        nameLabel = new JLabel("你的名字: " + gameLogic.getName());
        ageLabel = new JLabel("当前年龄: " + gameLogic.getAge());
        healthLabel = new JLabel("健康: " + gameLogic.getHealth());
        wealthLabel = new JLabel("财富: " + gameLogic.getWealth());
        happinessLabel = new JLabel("幸福度: " + gameLogic.getHappiness());
        marriageLabel = new JLabel("婚姻状态: " + (gameLogic.isMarried() ? "已婚" : "未婚"));
        spouseLabel = new JLabel("配偶名字: " + (gameLogic.isMarried() ? gameLogic.getSpouseName() : "无"));
        childrenLabel = new JLabel("子女数量: " + gameLogic.getChildrenCount());
        employmentLabel = new JLabel("就业状态: " + (gameLogic.isEmployed() ? "已就业" : "失业"));
        infoPanel.add(nameLabel);
        infoPanel.add(ageLabel);
        infoPanel.add(healthLabel);
        infoPanel.add(wealthLabel);
        infoPanel.add(happinessLabel);
        infoPanel.add(marriageLabel);
        infoPanel.add(spouseLabel);
        infoPanel.add(childrenLabel);
        infoPanel.add(employmentLabel);

        // 事件文本区域
        eventTextArea = new JTextArea(15, 60);
        eventTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(eventTextArea);

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        JButton studyButton = new JButton("学习");
        JButton workButton = new JButton("工作");
        JButton restButton = new JButton("休息");
        JButton findPartnerButton = new JButton("寻找伴侣");
        buttonPanel.add(studyButton);
        buttonPanel.add(workButton);
        buttonPanel.add(restButton);
        buttonPanel.add(findPartnerButton);

        // 学习按钮监听器
        studyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String result = gameLogic.study();
                eventTextArea.append(result);
                updateInfoLabels();
            }
        });

        // 工作按钮监听器
        workButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String result = gameLogic.work();
                eventTextArea.append(result);
                updateInfoLabels();
            }
        });

        // 休息按钮监听器
        restButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String result = gameLogic.rest();
                eventTextArea.append(result);
                updateInfoLabels();
            }
        });

        // 寻找伴侣按钮监听器
        findPartnerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String result = gameLogic.findPartner();
                eventTextArea.append(result);
                updateInfoLabels();
            }
        });

        add(infoPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        setVisible(true);
    }

    // 更新信息标签
    private void updateInfoLabels() {
        nameLabel.setText("你的名字: " + gameLogic.getName());
        ageLabel.setText("当前年龄: " + gameLogic.getAge());
        healthLabel.setText("健康: " + gameLogic.getHealth());
        wealthLabel.setText("财富: " + gameLogic.getWealth());
        happinessLabel.setText("幸福度: " + gameLogic.getHappiness());
        marriageLabel.setText("婚姻状态: " + (gameLogic.isMarried() ? "已婚" : "未婚"));
        spouseLabel.setText("配偶名字: " + (gameLogic.isMarried() ? gameLogic.getSpouseName() : "无"));
        List<String> childrenNames = gameLogic.getChildrenNames();
        StringBuilder childrenNameStr = new StringBuilder();
        for (String name : childrenNames) {
            childrenNameStr.append(name).append(", ");
        }
        if (childrenNameStr.length() > 0) {
            childrenNameStr.delete(childrenNameStr.length() - 2, childrenNameStr.length());
        }
        childrenLabel.setText("子女数量: " + gameLogic.getChildrenCount() + "，子女名字: " + childrenNameStr);
        employmentLabel.setText("就业状态: " + (gameLogic.isEmployed() ? "已就业" : "失业"));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LifeSimulatorGUI();
            }
        });
    }
}